<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
model('jy_weishop/member')->checklogin();
//注册电商客会员
if($_RQ['op']=='main'){
	$item=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','avatar','nickname'));
	$item['id']=$_SESSION['uid'];
	$user=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$item['level'] =pdo_getcolumn('jy_weishop_member_level',array('id'=>$user['level'],'uniacid'=>$_SESSION['uniacid']),'title');
	$item['level'] = $item['level']?$item['level']:'普通会员';
	if($_SESSION['os']=='windows'){
		$return['setting']=$this->plugin_setting('basic');
		$return['member']=$item;
		exi($return);
	}else{
		$return['setting']=$this->plugin_setting(basic);
		foreach($return['setting']['footernav'] as &$fv){
			$fv['url']=$fv['action'];
		}
	}
	//预估赚
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$lastdaytime = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
	$monthtime=mktime(0,0,0,date('m'),1,date('Y'));
	$lastmonthtime=strtotime('-1 month');
	$yearstime=strtotime(date("Y",time())."-1"."-1");
	$lastyearstime=strtotime(date("Y",time())."-2"."-1");
	$commission_total=pdo_getsum('jy_weishopke_order',array('mid'=>$_SESSION['uid']),'actualCosPrice');
	$return['fee_all']=price($commission_total);
	$commission_today=pdo_getsum('jy_weishopke_order',array('mid'=>$_SESSION['uid'],'createtime'=>$daytime),'actualCosPrice');
	$return['fee_today']=price($commission_today);
	$commission_lasttoday=pdo_getsum('jy_weishopke_order',array('mid'=>$_SESSION['uid'],'createtime between'=>array($lastdaytime,$daytime)),'actualCosPrice');
	$return['fee_lasttoday']=price($commission_lasttoday);
	$commission_mon=pdo_getsum('jy_weishopke_order',array('mid'=>$_SESSION['uid'],'createtime'=>$monthtime),'actualCosPrice');
	$return['fee_mon']=price($commission_mon);
	$commission_lastmon=pdo_getsum('jy_weishopke_order',array('mid'=>$_SESSION['uid'],'createtime between'=>array($lastmonthtime,$monthtime)),'actualCosPrice');
	$return['fee_lastmon']=price($commission_lastmon);
	$my_days = array(
				array('title'=>'我的优惠券','icon'=>'icon-youhuiquan','color'=>'#cc99ff','action'=>'jy_weishop/coupon.myindex/'),
				array('title'=>'微信会员卡','icon'=>'icon-daifahuo','color'=>'#cc99ff','action'=>'core/member.wxcard/main/'),
				array('title'=>'余额提现','icon'=>'icon-dixiaofei','color'=>'#cc99ff','action'=>'core/member.tixian/'),
				array('title'=>'地址管理','icon'=>'icon-position','color'=>'#cc99ff','action'=>'core/member.address/'),
				array('title'=>'帮助中心','icon'=>'icon-help','color'=>'#cc99ff','action'=>'jy_weishop/news.list/'),
				array('title'=>'获得购物卡','icon'=>'icon-qiangdan','color'=>'#cc99ff','action'=>'jy_weishop/member.getcard/'),
				array('title'=>'我的购物车','icon'=>'icon-gouwuche','color'=>'#cc99ff','action'=>'jy_weishop/member.cart/'),
				array('title'=>'我的购物卡','icon'=>'icon-pingzhengzhongxin','color'=>'#cc99ff','action'=>'jy_weishop/member.mycard/'),
				array('title'=>'我的关注','icon'=>'icon-woguanzhudepinpai','color'=>'#cc99ff','action'=>'jy_weishop/member.favorite/'),
				array('title'=>'我的足迹','icon'=>'icon-zuji','color'=>'#cc99ff','action'=>'jy_weishop/member.history/'),
	);
	$return['setting']['my_days'] = !empty($return['setting']['my_days']) ? $return['setting']['my_days'] : $my_days;
	$my_logs = array(
				array('title'=>'积分日志','icon'=>'icon-jifen','color'=>'#cc99ff','action'=>'core/member.credit_log/'),
				array('title'=>'余额日志','icon'=>'icon-jiangjiatongzhi','color'=>'#cc99ff','action'=>'core/member.credit_log/'),
				array('title'=>'提现日志','icon'=>'icon-trade','color'=>'#cc99ff','action'=>'core/member.cash_log/'),
				array('title'=>'红包日志','icon'=>'icon-ai-hongbao','color'=>'#cc99ff','action'=>'core/member.red_log/'),
	);
	$return['setting']['my_logs'] = !empty($return['setting']['my_logs']) ? $return['setting']['my_logs'] : $my_logs;
	$my_memberbtn = array(
				array('title'=>'领取优惠券','icon'=>'icon-youhuiquan1','color'=>'#cc99ff','action'=>'jy_weishop/coupon.index/'),
				array('title'=>'领取微信会员卡','icon'=>'icon-huiyuanqia','color'=>'#cc99ff','action'=>'core/member.wxcard/main'),
				array('title'=>'积分排行','icon'=>'icon-paixing','color'=>'#cc99ff','action'=>'jy_weishop/member.rank/'),
	);
	$return['setting']['my_memberbtn'] = !empty($return['setting']['my_memberbtn']) ? $return['setting']['my_memberbtn'] : $my_memberbtn;
	$return['item']=$item;
	$return['user']=$user;
	$ordernum = pdo_count('jy_weishopke_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$return['ordernum']=$ordernum;
	$return['closedsales']=1;
	$return['menu_list']=$menu_list;
	$current_plugin=$_SESSION['current_plugin']?:'jy_weishop';
	$sharelink=$current_plugin.'/index//fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
				'title'=>$setting['share_title']?$setting['share_title']:'首页-在线商城',
				'desc'=>$setting['share_desc']?$setting['share_desc']:'在线商城，一个属于自己的个人商城！',
				'link'=>custom_url($sharelink,true),
				'imgUrl'=>ATTACHMENT_URL.$setting['share_image']
	);
	set_html('title','会员中心');
	exi($return);
}
//购买会员
if($_RQ['op']=='buyvip'){
	set_html('title','购买会员');
	$basic=$this->plugin_setting('basic');
	$post=array();
	$post['packtype']=1;
	$post['packnum']=1;
	$post['checked']=true;
	$return['systemxieyi']=$this->plugin_setting('systemxieyi');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$packnum=$postdata['packnum'];
		$packtype=$postdata['packtype'];
		if($packtype==1){
			$fee=$basic['mfee']*$packnum;
		}elseif($packtype==2){
			$fee=$basic['jfee']*$packnum;	
		}elseif($packtype==3){
			$fee=$basic['yfee']*$packnum;
		}
		//创建一个会员订单
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'packnum'=>$packnum,
			'ordersn'=>get_ordersn('jy_weishop_viporder','ordersn'),
			'fee'=>$fee,
			'packtype'=>$packtype,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_weishop_viporder',$insert);
		$id=pdo_insertid();
		$params['fee']=$fee;
		$params['title']='购买会员订单';
		$params['id']=$id;
		$params['plugin']='jy_weishopke';
		$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		$params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
		$params['back_url']='jy_weishopke/member.index/payresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);
	}
	$return['post']=$post;
	$return['fee']=price($basic['mfee']);
	$return['fee0']=$basic['mfee0']?price($basic['mfee0']):price($basic['mfee']*1.5);
	$basic=$this->plugin_setting('basic');
	$return['xieyi']=$this->plugin_setting('systemxieyi');
	$return['vipbg']=$basic['vipbg'];
	exi($return);
}

if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_weishop_viporder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买会员订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！购买会员订单金额不匹配！','error');
	}
	pdo_update('jy_weishop_viporder',array('status'=>1,'ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['id']));
	//计算到期时间
	$startTime =time();
	$user=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
	if($order['packtype']==3){
		$order['packnum']=$order['packnum']*12;
	}elseif($order['packtype']==2){
		$order['packnum']=$order['packnum']*3;
	}
	if($user['viptime']!=0){
		
		$addTime=$user['viptime']+$order['packnum']*30*24*3600;
	}else{
		$addTime =$startTime+$order['packnum']*30*24*3600;
	}
	pdo_update('jy_weishop_member',array('isvip'=>1,'viptime'=>$addTime),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	//发送模板消息
	$this->notice('buyvip',$_SESSION['uid'],'',$order['id']);
	exi('支付成功','success','jy_weishopke/member.index/main/');
}	
?>